<!--
 * @Descripttion: (工序/Lm_Process)
 * @Author: (admin)
 * @Date: (2024-10-20)
-->
<template>
  <div>
    <el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent>
      <el-form-item label="工序编号" prop="processId">
        <el-input v-model="queryParams.processId" placeholder="请输入编号" />
      </el-form-item>
      <el-form-item label="工序名称" prop="processName">
        <el-input v-model="queryParams.processName" placeholder="请输入名称" />
      </el-form-item>
      <el-form-item label="类型" prop="processType">
        <el-select clearable  v-model="queryParams.processType" placeholder="请选择类型">
          <el-option v-for="item in  options.lm_process_type " :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue">
            <span class="fl">{{ item.dictLabel }}</span>
            <span class="fr" style="color: var(--el-text-color-secondary);">{{ item.dictValue }}</span>          
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
        <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
      </el-form-item>
    </el-form>
    <!-- 工具区域 -->
    <el-row :gutter="15" class="mb10">
      <el-col :span="1.5">
        <el-button type="primary" v-hasPermi="['lmprocess:add']" plain icon="plus" @click="handleAdd">
          {{ $t('btn.add') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-dropdown trigger="click" v-hasPermi="['lmprocess:import']">
          <el-button type="primary" plain icon="Upload">
            {{ $t('btn.import') }}<el-icon class="el-icon--right"><arrow-down /></el-icon>
          </el-button>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="upload">
                <importData
                  templateUrl="craft/lmProcess/importTemplate"
                  importUrl="/craft/lmProcess/importData"
                  @success="handleFileSuccess"></importData>
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="download" @click="handleExport" v-hasPermi="['lmprocess:export']">
          {{ $t('btn.export') }}
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table :data="dataList" v-loading="loading" ref="table" border
      header-cell-class-name="el-table-header-cell" highlight-current-row @sort-change="sortChange">
      <el-table-column prop="processId" label="编号" align="center" :show-overflow-tooltip="true" sortable v-if="columns.showColumn('processId')" width="90px"/>
      <el-table-column prop="processName" label="名称" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('processName')"/>
      <el-table-column prop="times" label="用时" align="center" v-if="columns.showColumn('times')" width="60px"/>
      <el-table-column prop="processType" label="类型" align="center" sortable v-if="columns.showColumn('processType')" width="120px">
        <template #default="scope">
          <dict-tag :options=" options.lm_process_type " :value="scope.row.processType"  />
        </template>
      </el-table-column>
      <el-table-column prop="operate" label="操作类型" align="center" sortable v-if="columns.showColumn('operate')" width="120px">
        <template #default="scope">
          <dict-tag :options=" options.lm_process_perate " :value="scope.row.operate"  />
        </template>
      </el-table-column>
      <el-table-column prop="testNum" label="测试数" align="center" v-if="columns.showColumn('testNum')" width="70px"/>
      <el-table-column prop="checkType" label="品检类型" align="center" v-if="columns.showColumn('checkType')" width="120px">
        <template #default="scope">
          <dict-tag :options=" options.lm_check_ype " :value="scope.row.checkType"  />
        </template>
      </el-table-column>
      <el-table-column prop="spotCheck" label="抽检类型" align="center" v-if="columns.showColumn('spotCheck')" width="120px">
        <template #default="scope">
          <dict-tag :options=" options.lm_spot_check " :value="scope.row.spotCheck"  />
        </template>
      </el-table-column>
      <el-table-column prop="material" label="物料类型" align="center" v-if="columns.showColumn('material')" width="120px">
        <template #default="scope">
          <dict-tag :options=" options.lm_material_type " :value="scope.row.material"  />
        </template>
      </el-table-column>
      <el-table-column prop="deviceID" label="设备编号" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('deviceID')" width="100px"/>
      <el-table-column prop="description" label="Description" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('description')"/>
      <el-table-column prop="create_by" label="创建用户" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('create_by')"/>
      <el-table-column prop="create_time" label="创建时间" :show-overflow-tooltip="true"  v-if="columns.showColumn('create_time')"/>
      <el-table-column prop="update_by" label="UpdateBy" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('update_by')"/>
      <el-table-column prop="update_time" label="UpdateTime" :show-overflow-tooltip="true"  v-if="columns.showColumn('update_time')"/>
      <el-table-column label="操作" width="120" align="center">
        <template #default="scope">
          <el-button type="success" size="small" icon="edit" title="编辑" v-hasPermi="['lmprocess:edit']" @click="handleUpdate(scope.row)"></el-button>
          <el-button type="danger" size="small" icon="delete" title="删除" v-hasPermi="['lmprocess:delete']" @click="handleDelete(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />

    <el-dialog :title="title" :lock-scroll="false" v-model="open" >
      <el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
        <el-row :gutter="20">
          <el-col :lg="12">
            <el-form-item label="工序编号" prop="processId">
              <el-input v-model="form.processId" placeholder="请输入工序编号" :disabled="opertype != 1" style="width: 120px"/>
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="工序名称" prop="processName">
              <el-input v-model="form.processName" placeholder="请输入工序名称" />
            </el-form-item>
          </el-col>           
          <el-col :lg="24">
            <el-form-item label="描述" prop="description">
              <el-input v-model="form.description" placeholder="请输入描述" />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="用时" prop="times" >
              <el-input v-model.number="form.times" placeholder="请输入用时" style="width: 120px"/>
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="工序类型" prop="processType">
              <el-select v-model="form.processType"  placeholder="请选择工序类型">
                <el-option
                  v-for="item in options.lm_process_type" 
                  :key="item.dictValue" 
                  :label="item.dictLabel" 
                  :value="item.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="操作类型" prop="operate">
              <el-select v-model="form.operate"  placeholder="请选择操作类型">
                <el-option
                  v-for="item in options.lm_process_perate" 
                  :key="item.dictValue" 
                  :label="item.dictLabel" 
                  :value="item.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>            
          <el-col :lg="12">
            <el-form-item label="测试数" prop="testNum">
              <el-input v-model.number="form.testNum" placeholder="请输入测试数" style="width: 200px"/>
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="品检类型" prop="checkType">
              <el-select v-model="form.checkType"  placeholder="请选择品检类型">
                <el-option
                  v-for="item in options.lm_check_ype" 
                  :key="item.dictValue" 
                  :label="item.dictLabel" 
                  :value="item.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="抽检类型" prop="spotCheck">
              <el-select v-model="form.spotCheck"  placeholder="请选择抽检类型">
                <el-option
                  v-for="item in options.lm_spot_check" 
                  :key="item.dictValue" 
                  :label="item.dictLabel" 
                  :value="item.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="物料类型" prop="material">
              <el-select v-model="form.material"  placeholder="请选择物料类型">
                <el-option
                  v-for="item in options.lm_material_type" 
                  :key="item.dictValue" 
                  :label="item.dictLabel" 
                  :value="item.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="设备编号" prop="deviceID">
              <el-input v-model="form.deviceID" placeholder="请输入设备编号" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer v-if="opertype != 3">
        <el-button text @click="cancel">{{ $t('btn.cancel') }}</el-button>
        <el-button type="primary" @click="submitForm">{{ $t('btn.submit') }}</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="lmprocess">
import { listLmProcess,
 addLmProcess, delLmProcess, 
 updateLmProcess,getLmProcess, 
 } 
from '@/api/craft/lmprocess.js'
import importData from '@/components/ImportData'
const { proxy } = getCurrentInstance()
const ids = ref([])
const loading = ref(false)
const showSearch = ref(true)
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  sort: 'ProcessId',
  sortType: 'asc',
  processId: undefined,
  processName: undefined,
  processType: undefined,
})
const columns = ref([
  { visible: true, align: 'center', type: '', prop: 'processId', label: 'ProcessId'  ,showOverflowTooltip: true  },
  { visible: true, align: 'center', type: '', prop: 'processName', label: 'ProcessName'  ,showOverflowTooltip: true  },
  { visible: true, align: 'center', type: '', prop: 'times', label: '用时'   },
  { visible: true, align: 'center', type: 'dict', prop: 'processType', label: '类型'  ,showOverflowTooltip: true  ,dictType: 'lm_process_type' },
  { visible: true, align: 'center', type: 'dict', prop: 'operate', label: '操作类型'  ,showOverflowTooltip: true  ,dictType: 'lm_process_perate' },
  { visible: true, align: 'center', type: '', prop: 'testNum', label: '测试数'   },
  { visible: true, align: 'center', type: 'dict', prop: 'checkType', label: '品检类型'  ,showOverflowTooltip: true  ,dictType: 'lm_check_ype' },
  { visible: true, align: 'center', type: 'dict', prop: 'spotCheck', label: '抽检类型'  ,showOverflowTooltip: true  ,dictType: 'lm_spot_check' },
  { visible: false, align: 'center', type: 'dict', prop: 'material', label: '物料类型'  ,showOverflowTooltip: true  ,dictType: 'lm_material_type' },
  { visible: false, align: 'center', type: '', prop: 'create_by', label: 'CreateBy'  ,showOverflowTooltip: true  },
  { visible: false, align: 'center', type: '', prop: 'create_time', label: 'CreateTime'  ,showOverflowTooltip: true  },
  { visible: false, align: 'center', type: '', prop: 'update_by', label: 'UpdateBy'  ,showOverflowTooltip: true  },
  { visible: false, align: 'center', type: '', prop: 'update_time', label: 'UpdateTime'  ,showOverflowTooltip: true  },
  { visible: false, align: 'center', type: '', prop: 'description', label: 'Description'  ,showOverflowTooltip: true  },
  { visible: false, align: 'center', type: '', prop: 'deviceID', label: '设备编号'  ,showOverflowTooltip: true  },
  //{ visible: false, prop: 'actions', label: '操作', type: 'slot', width: '160' }
])
const total = ref(0)
const dataList = ref([])
const queryRef = ref()
const defaultTime = ref([new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)])


var dictParams = [
  "lm_process_type",
  "lm_process_perate",
  "lm_check_ype",
  "lm_spot_check",
  "lm_material_type",
]

proxy.getDicts(dictParams).then((response) => {
  response.data.forEach((element) => {
    state.options[element.dictType] = element.list
  })
})

function getList(){
  loading.value = true
  listLmProcess(queryParams).then(res => {
    const { code, data } = res
    if (code == 200) {
      dataList.value = data.result
      total.value = data.totalNum
      loading.value = false
    }
  })
}

// 查询
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

// 重置查询操作
function resetQuery(){
  proxy.resetForm("queryRef")
  handleQuery()
}
// 自定义排序
function sortChange(column) {
  var sort = undefined
  var sortType = undefined

  if (column.prop != null && column.order != null) {
    sort = column.prop
    sortType = column.order

  }
  queryParams.sort = sort
  queryParams.sortType = sortType
  handleQuery()
}

/*************** form操作 ***************/
const formRef = ref()
const title = ref('')
// 操作类型 1、add 2、edit 3、view
const opertype = ref(0)
const open = ref(false)
const state = reactive({
  single: true,
  multiple: true,
  form: {},
  rules: {
    processId: [{ required: true, message: "ProcessId不能为空", trigger: "blur"     }],
    processName: [{ required: true, message: "ProcessName不能为空", trigger: "blur"     }],
    processType: [{ required: true, message: "类型不能为空", trigger: "change"     }],
  },
  options: {
    // 类型 选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
lm_process_type: [],
    // 操作类型 选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
lm_process_perate: [],
    // 品检类型 选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
lm_check_ype: [],
    // 抽检类型 选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
lm_spot_check: [],
    // 物料类型 选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
lm_material_type: [],
  }
})

const { form, rules, options, single, multiple } = toRefs(state)

// 关闭dialog
function cancel(){
  open.value = false
  reset()
}

// 重置表单
function reset() {
  form.value = {
    processId: null,
    processName: null,
    times: null,
    processType: null,
    operate: null,
    testNum: null,
    checkType: null,
    spotCheck: null,
    material: null,
    create_by: null,
    create_time: null,
    update_by: null,
    update_time: null,
    description: null,
    deviceID: null,
  };
  proxy.resetForm("formRef")
}


// 添加按钮操作
function handleAdd() {
  reset();
  open.value = true
  title.value = '添加工序'
  opertype.value = 1
}
// 修改按钮操作
function handleUpdate(row) {
  reset()
  const id = row.processId || ids.value
  getLmProcess(id).then((res) => {
    const { code, data } = res
    if (code == 200) {
      open.value = true
      title.value = '修改工序'
      opertype.value = 2

      form.value = {
        ...data,
      }
    }
  })
}

// 添加&修改 表单提交
function submitForm() {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {

      if (form.value.processId != undefined && opertype.value === 2) {
        updateLmProcess(form.value).then((res) => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addLmProcess(form.value).then((res) => {
            proxy.$modal.msgSuccess("新增成功")
            open.value = false
            getList()
          })
      }
    }
  })
}

// 删除按钮操作
function handleDelete(row) {
  const Ids = row.processId || ids.value

  proxy
    .$confirm('是否确认删除参数编号为"' + Ids + '"的数据项？', "警告", {
      confirmButtonText: proxy.$t('common.ok'),
      cancelButtonText: proxy.$t('common.cancel'),
      type: "warning",
    })
    .then(function () {
      return delLmProcess(Ids)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess("删除成功")
    })
}


// 导入数据成功处理
const handleFileSuccess = (response) => {
  const { item1, item2 } = response.data
  var error = ''
  item2.forEach((item) => {
    error += item.storageMessage + ','
  })
  proxy.$alert(item1 + '<p>' + error + '</p>', '导入结果', {
    dangerouslyUseHTMLString: true
  })
  getList()
}

// 导出按钮操作
function handleExport() {
  proxy
    .$confirm("是否确认导出工序数据项?", "警告", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    })
    .then(async () => {
      await proxy.downFile('/craft/lmProcess/export', { ...queryParams })
    })
}

handleQuery()
</script>